package cn.zh.npro.Activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.npro.Adapter.CaAdpter;
import cn.zh.npro.Item.CaFItem;
import cn.zh.npro.MyApplication;
import cn.zh.npro.R;
import cn.zh.npro.Util.BaseUrl;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class ConActivity extends BaseActivity {
    ListView list;
    CaAdpter caAdpter=null;
    ArrayList<CaFItem> caFItems=new ArrayList<>();
    String type="0";
    MyApplication mapp;
    String uid="";
   ImageView mc_back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_con);
        type=getIntent().getStringExtra("type");
        mapp= (MyApplication) getApplication();
        uid=mapp.getSp().getString("uid","");
        initca();
        if (type.equals("0")){
            getdata();
        }else {
            getcodate();
          }

        }

    public void initca(){
        mc_back=findViewById(R.id.mc_back);
        mc_back.setOnClickListener(this);
        list=findViewById(R.id.list);
        caAdpter=new CaAdpter(ConActivity.this,caFItems);
        list.setAdapter(caAdpter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent(ConActivity.this,Order.class);
                intent.putExtra("ob",caFItems.get(i));
                setResult(0,intent);
                finish();
            }
          });

        }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.mc_back:
                finish();
                break;
          }
        }

    public void getdata(){
           OkHttpUtils.get(BaseUrl.GetGoodsWareHouseList)
                   .params("userId",uid)
                   .execute(new StringCallback() {
                       @Override
                       public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                           try {
                               JSONObject js=new JSONObject(s);
                               boolean State=js.getBoolean("State");
                               if (State){
                               JSONArray Res=js.getJSONArray("Res");
                               caFItems.clear();
                               for (int i=0;i<Res.length();i++){
                                JSONObject ji=Res.getJSONObject(i);
                                String WarehouseID=ji.getString("WarehouseID");
                                String WarehouseName=ji.getString("WarehouseName");
                                CaFItem caFItem=new CaFItem(WarehouseID,WarehouseName);
                                caFItems.add(caFItem);
                                }
                               }
                               caAdpter.notifyDataSetChanged();
                           } catch (JSONException e) {
                               e.printStackTrace();
                           }
                       }
                       @Override
                       public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                           super.onError(isFromCache, call, response, e);
                        }
                      });
                    }

    public void getcodate(){
        OkHttpUtils.get(BaseUrl.GetLogisticList)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        System.out.println("------------->配送数据==="+s);
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State) {
                                JSONArray Res = js.getJSONArray("Res");
                                caFItems.clear();
                                for (int i = 0; i < Res.length(); i++) {
                                    JSONObject ji = Res.getJSONObject(i);
                                    String LogisticID = ji.getString("LogisticID");
                                    String Name = ji.getString("Name");
                                    CaFItem caFItem = new CaFItem(LogisticID, Name);
                                    caFItems.add(caFItem);
                                }
                              }
                            caAdpter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
                }













   }
